package com.huobao.yunying.goodsdetail_page;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.view.titlebar.AbTitleBar;
import com.huobao.yunying.activitys.Act_Main;
import com.huobao.yunying.activitys.Act_Select_GuigeAndNum;
import com.huobao.yunying.activitys.Act_Tuwenxiangqing;
import com.huobao.yunying.activitys.Act_Zixuan_Daili;
import com.huobao.yunying.activitys_store.Act_Store;
import com.huobao.yunying.adapters.Detail_ImageAdapter_New;
import com.huobao.yunying.base.HuobaoApplication;
import com.huobao.yunying.entity.DetailCart_data;
import com.huobao.yunying.entity.DetailGoodsData;
import com.huobao.yunying.entity.DetailStoreData;
import com.huobao.yunying.entity.Detailprice_intervals;
import com.huobao.yunying.entity.MySeeHistory;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.zhangying.R;
import com.mcoy.snapscrollview.McoyScrollView;
import com.mcoy.snapscrollview.McoySnapPageLayout;

import org.simple.eventbus.EventBus;
import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;
import org.xutils.DbManager;
import org.xutils.x;

import java.io.Serializable;

/**
 * Created by Administrator on 2016/8/12.
 */
public class GoodsDetailPage implements McoySnapPageLayout.McoySnapPage,View.OnClickListener{
	private View gouwuche_view;

	// 商品id
	private String goodsId;
	// 商品数据
	private DetailGoodsData goodsData;
	// 店铺数据
	private DetailStoreData storeData;
	// 购物车
	private DetailCart_data cartData;
	/** 商品名字 */
	private TextView tv_name;
	/** 商品第二个名 */
	private TextView tv_second_name;
	/** 价格 */
	private TextView tv_price;
	/** 看过 */
	private TextView tv_seenum;
	/** 库存 */
	private TextView tv_kucunnum;
	/** 图文详情按钮 */
	private TextView tv_tuwen_detail;
	/** 店铺头像 */
	private ImageView imageview;
	/** 店铺名称 */
	private TextView tv_storename;
	/** 店铺积分 */
	private TextView tv_store_jifen;

	private TextView tv_chengxin;
	private TextView tv_qiye;
	private TextView tv_shiti;

	// 价格和查看人数那一行
	private LinearLayout llyt_price;

	/** 进入店铺按钮 */
	private TextView btn_go_store;
	/** 全店商品按钮 */
	private TextView btn_all_progect;
	/** 轮播图 */
	private ViewFlow m_ViewFlow;
	private CircleFlowIndicator viewflowindic;
	private RelativeLayout rtly;
	private CircleFlowIndicator m_CircleFlowIndicator;

	/** 批发数量的layout */
	private LinearLayout llyt_guige;

	/** 商品属性的那一行 */
	private LinearLayout llyt_spec;
	private TextView tv_spec;


	private View view_store;


	private String default_img;


	private AbTitleBar mAbTitleBar;
	private Activity context ;
	private View rootView ;
	private McoyScrollView mcoyScrollView = null;
	public GoodsDetailPage(Activity context , View rootView,AbTitleBar mAbTitleBar,DetailGoodsData goodsData,DetailStoreData storeData,DetailCart_data cartData){
		this.context = context;
		this.rootView = rootView;
		this.mAbTitleBar = mAbTitleBar;
		this.goodsData = goodsData;
		this.storeData = storeData;
		this.cartData = cartData;
		goodsId = goodsData.goods_id;
		initView();
		init();
	}

	private void initView(){
		EventBus.getDefault().register(this);
		mcoyScrollView = (McoyScrollView) this.rootView
				.findViewById(R.id.product_scrollview);
		view_store = rootView.findViewById(R.id.view_store);
		tv_spec = (TextView)rootView.findViewById(R.id.tv_spec);
		llyt_spec = (LinearLayout)rootView.findViewById(R.id.llyt_spec);
		llyt_guige = (LinearLayout)rootView.findViewById(R.id.llyt_guige);
		m_CircleFlowIndicator = (CircleFlowIndicator)rootView.findViewById(R.id.viewflowindic);
		rtly = (RelativeLayout)rootView.findViewById(R.id.rtly);
		viewflowindic = (CircleFlowIndicator)rootView.findViewById(R.id.viewflowindic);
		m_ViewFlow = (ViewFlow)rootView.findViewById(R.id.viewflow);
		btn_all_progect = (TextView)rootView.findViewById(R.id.btn_all_progect);
		btn_go_store = (TextView)rootView.findViewById(R.id.btn_go_store);
		llyt_price = (LinearLayout) rootView.findViewById(R.id.llyt_price);
		tv_shiti = (TextView)rootView.findViewById(R.id.tv_shiti);
		tv_qiye = (TextView)rootView.findViewById(R.id.tv_qiye);
		tv_chengxin = (TextView)rootView.findViewById(R.id.tv_chengxin);
		tv_store_jifen = (TextView)rootView.findViewById(R.id.tv_store_jifen);
		tv_storename = (TextView)rootView.findViewById(R.id.tv_storename);
		imageview = (ImageView) rootView.findViewById(R.id.imageview);
		tv_tuwen_detail = (TextView)rootView.findViewById(R.id.tv_tuwen_detail);
		tv_kucunnum = (TextView)rootView.findViewById(R.id.tv_kucunnum);
		tv_seenum = (TextView)rootView.findViewById(R.id.tv_seenum);
		tv_price = (TextView)rootView.findViewById(R.id.tv_price);
		tv_second_name = (TextView)rootView.findViewById(R.id.tv_second_name);
		tv_name = (TextView)rootView.findViewById(R.id.tv_name);
		gouwuche_view = rootView.findViewById(R.id.gouwuche_view);
	}

	@Override
	public View getRootView() {
		return rootView;
	}

	@Override
	public boolean isAtTop() {
		return true;
	}

	@Override
	public boolean isAtBottom() {
		int scrollY = mcoyScrollView.getScrollY();
		int height = mcoyScrollView.getHeight();
		int scrollViewMeasuredHeight = mcoyScrollView.getChildAt(0).getMeasuredHeight();

		if ((scrollY + height) >= scrollViewMeasuredHeight) {
			return true;
		}
		return false;
	}








	private void showDetail() {

		saveSeeHistory(goodsData.default_image);
		if (goodsData._spec_arr == null || goodsData._spec_arr.size() == 0) {
			llyt_spec.setVisibility(View.GONE);
		} else {
			llyt_spec.setVisibility(View.VISIBLE);
			tv_spec.append(goodsData.spec_name_1 + ":");
			for (String str : goodsData._spec_arr) {
				tv_spec.append(str + " ");
			}
		}
		tv_name.setText(goodsData.goods_name);
		tv_second_name.setText(goodsData.goods_subname);
		if (goodsData.unit_remark.equals("")) {
			tv_price.setText("价格￥" + goodsData.price + goodsData.unit);
		} else {
			tv_price.setText("价格￥" + goodsData.price + goodsData.unit + "(" + goodsData.unit_remark + ")");
		}

		tv_seenum.setText(goodsData.views + "人看过");
		tv_kucunnum.setText("库存 " + goodsData._stock);
		ImageUtils.loadimg(imageview, storeData.store_logo);
		tv_storename.setText(storeData.store_name);
		tv_store_jifen.setText(storeData.credit_value);
		showViewFlow();
		showGuige();
		showRenzheng();
		if (goodsData.genre.equals("3")) {
			showNoJinhuocheView();
		}
	}

	private void saveSeeHistory(String image) {
		HuobaoApplication application = (HuobaoApplication) context.getApplication();
		DbManager db = x.getDb(application.daoConfig);

		try{
			long count = db.selector(MySeeHistory.class).where("goods_id","=",goodsData.goods_id).count();
			if(count == 0){
				MySeeHistory history = new MySeeHistory();
				history.setDefault_img(image);
				history.setGenre(goodsData.genre);
				history.setGoods_id(goodsData.goods_id);
				history.setGoods_name(goodsData.goods_name);
				history.setPrice(goodsData.price);
				db.save(history);
			}
		}catch (Exception e){}

	}

	/**
	 * 显示没有进货车的View
	 */
	private void showNoJinhuocheView() {
		llyt_price.setVisibility(View.GONE);
		llyt_guige.setVisibility(View.GONE);
	}

	/**
	 * 显示认证
	 */
	private void showRenzheng() {
		// 实体认证
		if (storeData.approve) {
			tv_shiti.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon3, 0, 0, 0);
			tv_qiye.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon2, 0, 0, 0);
		} else {
			tv_shiti.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon3a, 0, 0, 0);
			tv_qiye.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon2a, 0, 0, 0);
		}
		// 诚信保障
		if (storeData.indemnity) {
			tv_chengxin.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon1, 0, 0, 0);
		} else {
			tv_chengxin.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon1a, 0, 0, 0);
		}
		// 企业认证
	}

	/**
	 * 显示规格
	 */
	private void showGuige() {
		llyt_guige.removeAllViews();
		if (goodsData.price_intervals_count.equals("0")) {
			View view = LayoutInflater.from(context).inflate(R.layout.item_guige, null);
			TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
			TextView tv_price = (TextView) view.findViewById(R.id.tv_price);
			tv_number.setText("≥ 1 " + goodsData.unit);
			try{
				tv_price.setText("￥" + goodsData.price+"--"+"￥"+goodsData._spec_json.get(0).get(goodsData._spec_json.get(0).size()-1).get(1));
			}catch(Exception e){
				tv_price.setText("￥" + goodsData.price);
			}

			llyt_guige.addView(view);
		} else {
			for (Detailprice_intervals intervals : goodsData.price_intervals) {
				View view = LayoutInflater.from(context).inflate(R.layout.item_guige, null);
				view.setPadding(10, 0, 10, 0);
				TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
				TextView tv_price = (TextView) view.findViewById(R.id.tv_price);
				if (intervals.max_count == 0) {
					tv_number.setText("≥ " + intervals.min_count + goodsData.unit);
				} else {
					tv_number.setText(intervals.min_count + "-" + intervals.max_count + goodsData.unit);
				}
				tv_price.setText("￥" + intervals.price);
				llyt_guige.addView(view);
			}
		}
	}

	private void showViewFlow() {
		Detail_ImageAdapter_New imageadapter = new Detail_ImageAdapter_New(context, goodsData._images, rtly);
		m_ViewFlow.setAdapter(imageadapter);
		m_ViewFlow.setViewGroup(mcoyScrollView);
		m_ViewFlow.setTimeSpan(5000);
		m_ViewFlow.setSideBuffer(goodsData._images.size());
		m_ViewFlow.setFlowIndicator(m_CircleFlowIndicator);
		m_ViewFlow.startAutoFlowTimer();
	}



	protected void init() {
		llyt_spec.setOnClickListener(this);
		view_store.setOnClickListener(this);
		btn_go_store.setOnClickListener(this);
		btn_all_progect.setOnClickListener(this);
		showDetail();
	}


	/**
	 * 进入店铺点击
	 *
	 */
	private void onGoStoreClick() {
		Intent intent = new Intent(context, Act_Store.class);
		intent.putExtra("id", storeData.store_id);
		context.startActivity(intent);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_tuwen_detail:// 图文详情
				Intent intent = new Intent(context, Act_Tuwenxiangqing.class);
				intent.putExtra("data", goodsData);
				intent.putExtra("storeData", storeData);
				intent.putExtra("cartData", cartData);
				intent.putExtra("id", goodsData.goods_id);
				intent.putExtra("description", goodsData.description);
//			intent.putParcelableArrayListExtra("images", (ArrayList<? extends Parcelable>) goodsData._images);
//			intent.putParcelableArrayListExtra("props", (ArrayList<? extends Parcelable>) goodsData.props);
				intent.putExtra("images",(Serializable) goodsData._images);
				intent.putExtra("props",(Serializable) goodsData.props);
				context.startActivity(intent);
				// overridePendingTransition(R.anim.push_up_in,
				// R.anim.push_up_out);
				break;
			case R.id.llyt_spec:// 商品属性
				Intent in = new Intent(context, Act_Select_GuigeAndNum.class);
				in.putExtra("data", goodsData);
				in.putExtra("gouwuche", cartData.cart_num);
				context.startActivity(in);
				break;
			case R.id.tv_tel:
				break;
			case R.id.tv_save:
				break;
			case R.id.gouwuche_view:// 跳转到购物车界面
				intent = new Intent(context, Act_Main.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				EventBus.getDefault().post("2", "ShowGouwuche");
				context.startActivity(intent);
				context.finish();
				break;
			case R.id.tv_jinhuozhe://加入购物车按钮
				Intent in1 = new Intent(context, Act_Select_GuigeAndNum.class);
				in1.putExtra("data", goodsData);
				in1.putExtra("gouwuche", cartData.cart_num);
				context.startActivity(in1);
				break;
			case R.id.tv_dinggou:
				if (goodsData.genre.equals("3")){
					intent = new Intent(context, Act_Zixuan_Daili.class);
					intent.putExtra("id", goodsId);
					intent.putExtra("sid", storeData.store_id);
					context.startActivity(intent);
				}else{
					in1 = new Intent(context, Act_Select_GuigeAndNum.class);
					in1.putExtra("data", goodsData);
					in1.putExtra("gouwuche", cartData.cart_num);
					context.startActivity(in1);
				}
				break;
			case R.id.btn_go_store:
			case R.id.btn_all_progect:
				onGoStoreClick();
				break;
			case R.id.view_store:
				intent = new Intent(context, Act_Store.class);
				intent.putExtra("id",storeData.store_id);
				context.startActivity(intent);
				break;
		}
	}

}
